package com.csy.user.entity.owner.service.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csy.user.entity.owner.domain.AjOwnerRoom;
import com.csy.user.entity.owner.domain.AjOwnerRoomRecord;
import com.csy.user.entity.owner.domain.vo.AjOwnerRoomRecordVo;
import com.csy.user.entity.owner.domain.vo.AjOwnerRoomVo;
import com.csy.user.entity.owner.mapper.AjOwnerRoomMapper;
import com.csy.user.entity.owner.service.AjOwnerRoomService;

import cn.hutool.core.util.IdUtil;

@Service
public class AjOwnerRoomServiceImpl implements AjOwnerRoomService {

    @Autowired
    AjOwnerRoomMapper ajOwnerRoomMapper;

    @Override
    public List<AjOwnerRoomVo> getOwnerRoomList(AjOwnerRoomVo ajOwnerRoomVo) {
        return ajOwnerRoomMapper.getOwnerRoomList(ajOwnerRoomVo);
    }

    @Override
    public int addOwnerRoom(AjOwnerRoom ajOwnerRoom) {
        return ajOwnerRoomMapper.addOwnerRoom(ajOwnerRoom);
    }

    @Override
    public List<AjOwnerRoomRecordVo> getOwnerRoomRecords(String ownerRoomId) {
        return ajOwnerRoomMapper.getOwnerRoomRecords(ownerRoomId);
    }

    @Override
    public int ownerDel(Long ownerId) {
        return ajOwnerRoomMapper.ownerRoomDel(ownerId);
    }

    @Override
    @Transactional
    public int bindingRoom(AjOwnerRoomVo ownerRoom) {
        String roomStatus = ownerRoom.getRoomStatus();
        //绑定房屋信息
        if ("3".equals(roomStatus)){
            ownerRoom.setRoomStatus("0");

            return ajOwnerRoomMapper.updateOwnerRoom(ownerRoom);
        }
        if ("1".equals(roomStatus)){
            //1.修改绑定状态为已绑定
            //2.审核信息，审核记录
            AjOwnerRoomRecord ajOwnerRoomRecord = new AjOwnerRoomRecord();
            ajOwnerRoomRecord.setOwnerRoomId(ownerRoom.getOwnerRoomId()+"");
            ajOwnerRoomRecord.setRecordId(IdUtil.getSnowflake(1,1).nextId());
            ajOwnerRoomRecord.setCommunityId(ownerRoom.getCommunityId());
            ajOwnerRoomRecord.setBuildingId(ownerRoom.getBuildingId());
            ajOwnerRoomRecord.setUnitId(ownerRoom.getUnitId());
            ajOwnerRoomRecord.setRoomId(ownerRoom.getRoomId());
            ajOwnerRoomRecord.setOwnerId(ownerRoom.getOwnerId());
            ajOwnerRoomRecord.setOwnerType(ownerRoom.getOwnerType());
            ajOwnerRoomRecord.setRecordAuditOpinion(ownerRoom.getRecordAuditOpinion());
            ajOwnerRoomRecord.setRoomStatus(ownerRoom.getRoomStatus());
            ajOwnerRoomRecord.setCreateTime(new Date());
            ajOwnerRoomRecord.setCreateBy(ownerRoom.getCreateBy());
            ajOwnerRoomMapper.insertRecord(ajOwnerRoomRecord);
            return ajOwnerRoomMapper.updateOwnerRoom(ownerRoom);
        }
        return 0;
    }

    @Override
    public int unBindOwner(AjOwnerRoomVo ownerRoom) {

        ownerRoom.setRoomStatus("3");
        ownerRoom.setCommunityId(1l);
        ownerRoom.setBuildingId(1l);
        ownerRoom.setUnitId(1l);
        ownerRoom.setRoomId(1l);

        return ajOwnerRoomMapper.updateOwnerRoom(ownerRoom);
    }
}
